﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CollisionDetectionSandbox
{
    public class Enemy
    {
        public ElementType Type { get; set; }
        public Vector2 Location { get; set; }
        public Vector2 Size { get { return SIZE; } }

        public float Left { get { return Location.X - (Size.X / 2f); } }
        public float Right { get { return Location.X + (Size.X / 2f); } }
        public float Top { get { return Location.Y - (Size.Y / 2f); } }
        public float Bottom { get { return Location.Y + (Size.Y / 2f); } }

        private static readonly Vector2 SIZE = new Vector2(40, 40);
        public static float ENEMY_SPEED = .25f;//pixels per update

        public static List<Enemy> ObjectList = new List<Enemy>();

        public Enemy(ElementType type, Vector2 location)
        {
            Type = type;
            Location = location;
        }

        public void MoveOneStep()
        {
            //get unit vector in direction of enemy travel
            Vector2 unit = new Vector2(0, -1 * ENEMY_SPEED);

            //add to current location
            Location = new Vector2(Location.X + unit.X, Location.Y - unit.Y);
        }

        public bool IsHittingBottom()
        {
            bool bHitting = (this.Bottom >= GameScreen.PlayerLocation.Y);

            return bHitting;
        }

        public void Delete()
        {
            ObjectList.Remove(this);
        }
        
        static int seed = 0;
        public static void SpawnEnemies()
        {            
            float x = SIZE.X / 2f + 120;

            while (x <= GameScreen.GAME_WIDTH - (SIZE.X / 2f) - 110)
            {
                Vector2 loc = new Vector2(x, -SIZE.Y);

                Random r = new Random(seed++ * (int)DateTime.Now.Ticks);
                ElementType type = (ElementType)r.Next(4);

                Enemy e = new Enemy(type, loc);
                ObjectList.Add(e);

                x += SIZE.X;
            }
        }

        public void Draw(SpriteBatch batch)
        {
            Texture2D texture;

            switch (Type)
            {
                case ElementType.Fire:
                    texture = GameScreen.FireTexture;
                    break;
                case ElementType.Earth:
                    texture = GameScreen.EarthTexture;
                    break;
                case ElementType.Water:
                    texture = GameScreen.WaterTexture;
                    break;
                default:
                    texture = GameScreen.LightTexture;
                    break;
            }

            //it wants the top left corner for location, not the center
            Vector2 drawLoc = new Vector2(Left, Top);
            batch.Draw(texture, drawLoc, Color.White);
        }
    }
}
